let express = require('express');
let router = express.Router();

const consultService = require('../service/consultService');
const checkJWTMiddleWare = require('../middleWare/checkJWTMiddleWare');

// 此路由需要登录后才能访问
router.use(checkJWTMiddleWare);

/*
 * 查询所有咨询记录接口
 */
router.get('/getAllConsultList', async function (req, res, next) {
    let result = await consultService.getAllConsultList(req.jwtResult.data.uid, req.jwtResult.data.userType);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 新增咨询记录接口
 */
router.post('/addConsultRecord', async function (req, res, next) {
    let studentId = req.body.studentId;
    let startTime = req.body.startTime;
    let endTime = req.body.endTime;
    let issueTypeId = req.body.issueTypeId;
    let consultationTopic = req.body.consultationTopic;
    let counselorId = req.body.counselorId;
    let isWarning = req.body.isWarning;
    let subjectiveDescription = req.body.subjectiveDescription;
    let objectiveDescription = req.body.objectiveDescription;
    let counselorAssessment = req.body.counselorAssessment;
    let counselorNotes = '';
    let consultationMethodId = req.body.consultationMethodId;
    let handlingMethod = req.body.handlingMethod;
    let isClose = 'false';
    // 非空参数
    if (!studentId || !startTime || !endTime || !issueTypeId || !consultationTopic || !counselorId || !isWarning || !consultationMethodId || !isClose) {
        res.status(400).json({ success: false, msg: "参数不全，无法添加记录" });
        return ;
    }
    // 可选参数
    subjectiveDescription = subjectiveDescription ? subjectiveDescription : "";
    objectiveDescription = objectiveDescription ? objectiveDescription : "";
    counselorAssessment = counselorAssessment ? counselorAssessment : "";
    counselorNotes = counselorNotes ? counselorNotes : "";
    // 构建数据
    let data = {
        studentId, startTime, endTime, issueTypeId,
        consultationTopic, counselorId, isWarning,
        subjectiveDescription, objectiveDescription, counselorAssessment,
        counselorNotes, consultationMethodId, handlingMethod, isClose
    };
    // 调用服务层
    let result = await consultService.addConsultRecord(req.jwtResult.data.uid, req.jwtResult.data.userType, data);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 查看单个咨询记录接口
 */
router.post('/consultRecordInfo', async function (req, res, next) {
    let consultationId = req.body.consultationId;
    // 非空参数
    if (!consultationId) {
        res.status(400).json({ success: false, msg: "参数不全，无法查看" });
        return ;
    }
    // 调用服务层
    let result = await consultService.getConsultRecordInfo(req.jwtResult.data.uid, req.jwtResult.data.userType, consultationId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 查看某个学生的咨询记录接口
 */
router.post('/getConsultRecordByStudent', async function (req, res, next) {
    let studentId = req.body.studentId;
    // 非空参数
    if (!studentId) {
        res.status(400).json({ success: false, msg: "参数不全，无法查询" });
        return ;
    }
    // 调用服务层
    let result = await consultService.getConsultRecordByStudent(req.jwtResult.data.uid, req.jwtResult.data.userType, studentId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 修改咨询记录接口
 */
router.post('/updateConsultRecordInfo', async function (req, res, next) {
    let consultationId = req.body.consultationId;
    let studentId = req.body.studentId;
    let startTime = req.body.startTime;
    let endTime = req.body.endTime;
    let issueTypeId = req.body.issueTypeId;
    let consultationTopic = req.body.consultationTopic;
    let counselorId = req.body.counselorId;
    let isWarning = req.body.isWarning;
    let subjectiveDescription = req.body.subjectiveDescription;
    let objectiveDescription = req.body.objectiveDescription;
    let counselorAssessment = req.body.counselorAssessment;
    let counselorNotes = req.body.counselorNotes;
    let consultationMethodId = req.body.consultationMethodId;
    let handlingMethod = req.body.handlingMethod;
    let isClose = req.body.isClose;
    // 非空参数
    if (!consultationId || !studentId || !startTime || !endTime || !issueTypeId || !consultationTopic || !counselorId || !isWarning || !subjectiveDescription || !objectiveDescription || !counselorAssessment || !counselorNotes || !consultationMethodId || !handlingMethod || !isClose) {
        res.status(400).json({ success: false, msg: "参数不全，无法修改" });
        return ;
    }
    // 构建数据
    let data = {
        studentId, startTime, endTime, issueTypeId,
        consultationTopic, counselorId, isWarning,
        subjectiveDescription, objectiveDescription, counselorAssessment,
        counselorNotes, consultationMethodId, handlingMethod, isClose, consultationId
    };
    // 调用服务层
    let result = await consultService.updateConsultRecordInfo(req.jwtResult.data.uid, req.jwtResult.data.userType, data);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 删除咨询记录接口
 */
router.post('/deleteConsultRecord', async function (req, res, next) {
    let consultationId = req.body.consultationId;
    if (!consultationId) {
        res.status(400).json({ success: false, msg: "参数不全，无法修改" });
        return ;
    }
    let result = await consultService.deleteConsultRecord(req.jwtResult.data.uid, req.jwtResult.data.userType, consultationId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 查看所有咨询师的接口
 */
router.get('/getAllCounselorList', async function (req, res, next) {
    let result = await consultService.getAllCounselorList();
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 查看所有咨询方式的接口
 */
router.get('/getAllConsultationMethods', async function (req, res, next) {
    let result = await consultService.getAllConsultationMethods();
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 查看所有问题类型接口
 */
router.get('/getAllIssueTypes', async function (req, res, next) {
    let result = await consultService.getAllIssueTypes();
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 查看所有咨询主题的接口
 */
// router.get('/getAllConsultationTopics', async function (req, res, next) {
//     let result = await consultService.getAllConsultationTopics();
//     res.status(result.success ? 200 : 400).json(result);
// });

module.exports = router;
